package com.example.liujie.hellolibgdx

import com.badlogic.gdx.ApplicationListener
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.graphics.Pixmap
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.SpriteBatch

class PixmapDemo : ApplicationListener {

    lateinit var spriteBatch: SpriteBatch
    lateinit var texture: Texture
    lateinit var pixmap: Pixmap

    override fun render() {
        // 黑色清屏
        Gdx.gl.glClearColor(0f, 0f, 0f, 1f)
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)

        spriteBatch.begin()

        // 在屏幕左下角绘制纹理
        spriteBatch.draw(texture, 0f, 0f)

        spriteBatch.end()
    }

    override fun pause() {

    }

    override fun resume() {

    }

    override fun resize(p0: Int, p1: Int) {
    }

    override fun create() {
        spriteBatch = SpriteBatch()
        pixmap = Pixmap(256, 256, Pixmap.Format.RGBA8888)
        // 设置绘图颜色为白色
        pixmap?.setColor(1F, 1f, 1f, 1f)
        // 将整个 pixmap 填充为当前设置的颜色
        pixmap?.fill()

        // 设置绘图颜色为绿色
        pixmap.setColor(Color.GREEN)
        // 画一条线段, 线段两点为 (0, 0) 到 (256, 128)
        pixmap.drawLine(0, 0, 256, 128)

        // 设置绘图颜色为蓝色
        pixmap.setColor(Color.BLUE)
        // 画一个矩形, 矩形左上角坐标(128, 128), 宽高均为64
        pixmap.drawRectangle(128, 128, 64, 64)

        // 设置绘图颜色为黄色
        pixmap.setColor(Color.YELLOW)
        // 填充一个三角形, 三点(0, 256), (0, 128), (128, 128)
        pixmap.fillTriangle(0, 256, 0, 128, 128, 128)

        // pixmap 处理完成后转换成纹理
        texture = Texture(pixmap)

        // pixmap 已不再需要用到, 释放资源
        pixmap.dispose()
    }

    override fun dispose() {
        spriteBatch.dispose()
        texture.dispose()
    }

}